package org.jeecg.modules.flow.callback.move;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import lombok.RequiredArgsConstructor;
import org.jeecg.common.api.vo.Result;
import org.jeecg.modules.business.third.entity.EmsOrDmsDeliveryDto;
import org.jeecg.modules.business.third.service.IEmsOrDmsDeliveryOperateService;
import org.jeecg.modules.business.transfer.entity.WmsMoveReceipt;
import org.jeecg.modules.business.transfer.service.IWmsMoveReceiptService;
import org.jeecg.modules.flow.sys.common.BaseCustomBeanRun;
import org.jeecg.modules.flow.sys.entity.dto.BaseFlowDto;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

/**
 * 纵剪叫纸筒-预警 通知叫纸筒 回调Bean
 *
 * @author lumingyi
 * @version 1.0.0
 * @since 2023-11-16
 */
@RequiredArgsConstructor
@Component
public class Y15MoveNoticeBean<T> extends BaseCustomBeanRun<T> {

    private final IEmsOrDmsDeliveryOperateService emsOrDmsDeliveryOperateService;

    private final IWmsMoveReceiptService moveReceiptService;

    /**
     * 业务逻辑 纵剪叫纸筒-预警 通知叫纸筒
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void actureRun() {
        BaseFlowDto baseFlowDto = this.getBaseFlowDto();
        String id = baseFlowDto.getInfoId();
        // 主动调用EMS发送叫纸筒请求
        EmsOrDmsDeliveryDto emsOrDmsDeliveryDto = new EmsOrDmsDeliveryDto();
        emsOrDmsDeliveryDto.setReceiptId(id);
        boolean successFlag = emsOrDmsDeliveryOperateService.outbound(emsOrDmsDeliveryDto);
//        baseFlowDto.setCurrentFailed(successFlag);

        // 更新即时库存
        moveReceiptService.moveRepertory(id);
        // 更新单据状态为已完成
        moveReceiptService.update(new LambdaUpdateWrapper<WmsMoveReceipt>()
                        .eq(WmsMoveReceipt::getId, id)
                        .set(WmsMoveReceipt::getDocumentStatus, 200));
        // 当前前端调用业务需要你返回的
        baseFlowDto.setResult(Result.OK());
        // id是保存业务记录的id。用于生成业务流程信息记录
        baseFlowDto.setInfoId(id);
    }
}
